import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


// 登录与注册
public class Login {
    Map<String, Object> account = new HashMap<String, Object>();

    public  Map<String, Object> login(Scanner input){                          //登录
        System.out.println("======欢迎进入登录界面======");
        System.out.println("请输入你的账号");
        String id=input.next();
        while(!isIdExist(id)){                            //判断账号id是否存在
            System.out.println("账号不存在! 请重新输入:");
            id=input.next();
        }
        this.account = getAccount(id);
        System.out.println("请输入你的密码");
        String password=input.next();
        while(!password.equals(this.account.get("password"))){     //判断账号密码是否正确
            System.out.println("密码错误! 请重新输入:");
            password=input.next();
        }

        System.out.println("登录成功!");
        return account;

    }

    public void register(Scanner input){                 //注册
        System.out.println("======欢迎进入注册界面======");
        System.out.println("请输入账号名称:");
        String id = input.next();
        while(FileOpera.JsonRead(id)!=null){           //昵称是否重复
            System.out.println("昵称重复!");
            id = input.next();
        }
        this.account.put("id",id);
        //输入密码
        System.out.println("请输入密码:");
        String password = input.next();
        System.out.println("请二次确认密码:");
        String SecondPassword = input.next();
        while(!SecondPassword.equals(password)){
            System.out.println("密码错误! 请重新输入:");
            SecondPassword = input.next();
        }
        this.account.put("password",password);
        if(id.equals("zcm")){this.account.put("root","super");}
        else this.account.put("root","normal");

        System.out.println("注册成功!");
        System.out.println("下面请完善个人信息:");
        System.out.println("真实姓名:");
        String name = input.next();
        this.account.put("name",name);

        System.out.println("年龄:");
        String age ;
        age = input.next();
        while(!isNumeric(age)){
            System.out.println("输入格式错误! ");
            System.out.println("年龄:");
            age = input.next();
        }

        this.account.put("age", Integer.parseInt(age));

        System.out.println("个性签名:");
        String motto = input.next();
        this.account.put("motto",motto);

        this.account.put("正确题数",0);

        FileOpera.JsonWrite(id,this.account);           //把账户信息写入json文件保存
        System.out.println("======提交成功======");

    }

    //判断输入年龄时是否为数字
    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    //判断账号id是否存在
    public static boolean isIdExist(String id){
        //未找到此ID
        return FileOpera.JsonRead(id) != null;
    }

    // 返回存储账号信息的map
    public Map<String,Object> getAccount(String id){ //读取账户信息返回
        return  FileOpera.JsonRead(id);
    }


}